<?php

namespace app\api\model;

use think\Model;

class Menu extends Model
{


    public function add($data)
    {

        $this->startTrans();
        try {
            $this->allowField(['sort','status','pid','meta','name','path','redirect','hidden','role','component','noCache'])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }


    public function edit($data)
    {
        $model = Menu::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField(['sort','status','pid','meta','name','path','redirect','hidden','role','component','noCache'])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }

    public function dele($id)
    {

        $model = Menu::find($id);
        $list = $model->where('pid',$id)->find();
        if($list){
            return re_error('请先删除子菜单');
        }
        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}